
package com.ifcar.app.adapter;

import com.ifcar.app.model.Vote;
import com.ifcar.app.util.BaseImageDownloader;
import com.ifcar.app.util.ModelUtils;
import com.ifcar.app.util.PicUtil;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.SimpleAdapter.ViewBinder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class VoteViewBinder implements ViewBinder {

    private BaseImageDownloader mImageDownloader;

    private ThemeSettingsHelper mThemeSettingsHelper;

    private Context mContext;

    private SimpleDateFormat mDateFromFormat = new SimpleDateFormat("yy-mm-dd");

    private SimpleDateFormat mDateToFormat = new SimpleDateFormat("mm月dd日");

    private int mW;

    private int mH;

    public VoteViewBinder(Context context) {
        mContext = context;
        mThemeSettingsHelper = ThemeSettingsHelper.getThemeSettingsHelper(context);
        mImageDownloader = new BaseImageDownloader(context, null);
        mImageDownloader.setMode(BaseImageDownloader.Mode.CORRECT);

        BitmapDrawable defaultNewsDrawable = (BitmapDrawable) mThemeSettingsHelper
                .getThemeDrawable(context, R.drawable.default_news);
        mImageDownloader.setDefaultBitmap(defaultNewsDrawable.getBitmap());
        final float density = context.getResources().getDisplayMetrics().density;
        // 注意下面长和宽要和布局文件中的图片长宽一致
        mW = (int) (density * 73);
        mH = (int) (density * 53);
        mImageDownloader.setBitmapWandH(mW, mH, null);
    }

    @Override
    public boolean setViewValue(View view, Object data, String textRepresentation) {
        int id = view.getId();
        switch (id) {
            case R.id.img:
                setImg((ImageView) view, (String) data);
                return true;
            case R.id.question:
                TextView queView = (TextView) view;
                String que = (String) data;
                mThemeSettingsHelper.setTextViewColor(mContext, queView, R.color.list_title_color);
                queView.setText(que);
                return true;
            case R.id.date:
                setDate((TextView) view, (String) data, false, true);
                return true;
            case R.id.num:
                setNum((TextView) view,
                        String.valueOf(Vote.getVoteTotalNum((List<Map<String, Object>>) data)));
                return true;
            default:
                break;
        }

        return false;
    }

    /**
     * 通过这个方法可以设置图片显示或者不显示
     * 
     * @param thread
     */
    public void setThread(Thread thread) {
        synchronized (mImageDownloader) {
            mImageDownloader.setThread(thread);
        }
    }

    public void bindView(View itemView, Map<String, Object> data, boolean isVoted, boolean over,
            OnClickListener l) {
        String imgSrc = (String) ModelUtils.getValue(data, StringUtil.VOTE_IMGSRC);
        String digest = (String) ModelUtils.getValue(data, StringUtil.VOTE_DIGEST);
        String date = (String) ModelUtils.getValue(data, StringUtil.VOTE_DATE);
        String totleNum = String.valueOf(Vote.getVoteTotalNum(data));
        String question = (String) ModelUtils.getValue(data, StringUtil.VOTE_QUESTION);

        ImageView imgView = (ImageView) itemView.findViewById(R.id.img);
        setImg(imgView, imgSrc);
        TextView digestView = (TextView) itemView.findViewById(R.id.digest);
        digestView.setText(digest);
        TextView dateView = (TextView) itemView.findViewById(R.id.date);
        setDate(dateView, date, isVoted, over);
        TextView totleNumView = (TextView) itemView.findViewById(R.id.num);
        setNum(totleNumView, totleNum);
        TextView queView = (TextView) itemView.findViewById(R.id.question);
        queView.setText(question);
        View digestContainer = itemView.findViewById(R.id.digest_container);
        digestContainer.setOnClickListener(l);
        digestContainer.setTag(data);
        
        mThemeSettingsHelper.setViewBackgroud(mContext, itemView.findViewById(R.id.vote_item), R.drawable.vote_item_bg);
        mThemeSettingsHelper.setViewBackgroud(mContext, itemView.findViewById(R.id.vote_question_container), R.drawable.vote_question_bg);
        mThemeSettingsHelper.setImageViewSrc(mContext, (ImageView)itemView.findViewById(R.id.vote_question_icon_f), R.drawable.vote_question_icon);
        mThemeSettingsHelper.setImageViewSrc(mContext, (ImageView)itemView.findViewById(R.id.arrow), R.drawable.vote_arrow);
        
        mThemeSettingsHelper.setTextViewColor(mContext, queView, R.color.list_title_color);
        mThemeSettingsHelper.setTextViewColor(mContext, dateView, R.color.list_desc_color);
        mThemeSettingsHelper.setTextViewColor(mContext, totleNumView, R.color.list_desc_color);
        mThemeSettingsHelper.setTextViewColor(mContext, digestView, R.color.vote_digest_color);
    }

    private void setImg(ImageView view, String url) {
        String imgUrl = PicUtil.get3G_PhotoResize(url, mW, mH);
        mImageDownloader.setBitmapWandH(mW, mH, "Y");
        mImageDownloader.download(imgUrl, view);
    }

    private void setNum(TextView view, String num) {
        view.setText(mContext.getString(R.string.vote_num, num));
        mThemeSettingsHelper.setTextViewColor(mContext, view, R.color.list_desc_color);
    }

    private void setDate(TextView view, String date, boolean isVoted, boolean over) {
        try {
            Date d = mDateFromFormat.parse(date);
            date = mDateToFormat.format(d);
        } catch (Exception e) {
        }

        String msg;
        if (isVoted) {
            msg = mContext.getString(R.string.vote_already);
        } else if (over) {
            msg = mContext.getString(R.string.vote_missing);
        } else {
            msg = mContext.getString(R.string.vote_ing);
        }
        if (TextUtils.isEmpty(date)) {
            view.setText(msg);
        } else {
            view.setText(date + msg);
        }
        mThemeSettingsHelper.setTextViewColor(mContext, view, R.color.list_desc_color);
    }
}
